package net.ziqiang.sdk.teagreen.util;

public class Teagreen {
	/**
	 * Tell whether a substring exists in a supstring,ignoring case.
	 * @param subString substring
	 * @param supString supstring
	 * @return boolean
	 */
	public static boolean isChildIgnoreCase(String subString,String supString){
		if((supString.toLowerCase()).indexOf(subString.toLowerCase())==-1)
			return false;
		else
			return true;
	}

	/**
	 * Tell whether a substring exists in a supstring, case sensitive.
	 * @param subString substring
	 * @param supString supstring
	 * @return boolean
	 */
	public static boolean isChild(String subString,String supString){
		if(supString.indexOf(subString)==-1)
			return false;
		else
			return true;
	}

	/**
	 * Convert a string form  ISO8859_1 to GB2312.
	 * @param strSrc string to convert
	 * @return 
	 */
	public static String convertToGB2312(String strSrc){
		try{
			if(strSrc==null)
				return null;
			strSrc=new String(strSrc.getBytes("ISO8859_1"),"gb2312");
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return strSrc;
	}

	/**
	 * Convert the charset of a string.
	 * @param strSrc string to convert
	 * @param oldCharset old charset
	 * @param newCharset new charset
	 * @return
	 */
	public static String convertCharset(String strSrc,String oldCharset,String newCharset){
		try{
			if(strSrc==null)
				return null;
			strSrc=new String(strSrc.getBytes(oldCharset),newCharset);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return strSrc;
	}

	/**
	 * Get a substring form a supstring from the start index to the end index
	 * @param supString supstring
	 * @param start start index
	 * @param end end index
	 * @return
	 */
	public static String cutSentence(String supString,int start,int end,String endStr){
		try{
			if(end>=supString.length()){
				return supString;
			}else{
				return supString.substring(start, end+1)+endStr;
			}
		}catch(IndexOutOfBoundsException e1){
			return null;
		}
	}

	/**
	 * Get a substring with a given length form a supstring from the beginning
	 * @param supString supstring
	 * @param length length
	 * @param endStr string to attach to the end of the substring
	 * @return
	 */
	public static String cutString(String supString,int length,String endStr){
		try{
			if(length>=supString.length()){
				return supString;
			}else{
				return supString.substring(0, length)+endStr;
			}
		}catch(IndexOutOfBoundsException e){
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Replace all the chars in a string with new chars
	 * @param originalString original string
	 * @param oldChar old char
	 * @param newChar new char
	 * @return
	 */
	public static String replaceChar(String originalString, char oldChar, char newChar) {
		return originalString.replace(oldChar, newChar) ;
	}}
